package com.corgi.mybatis.util;

import java.nio.charset.StandardCharsets;

/**
 * @title: Decimal
 * @description:
 * @author: dengmiao
 * @create: 2019-10-31 09:14
 **/
public class DecimalUtil {

    private static String hexStr =  "0123456789ABCDEF";
    private static String[] binaryArray = {
            "0000", "0001", "0010", "0011",
            "0100", "0101", "0110", "0111",
            "1000", "1001", "1010", "1011",
            "1100", "1101", "1110", "1111"
    };

    public static void main(String[] args) {
        String str = "二进制与十六进制互转测试";
        System.out.println("源字符串：\n"+str);

        String hexString = binaryToHexString(str.getBytes());
        System.out.println("转换为十六进制：\n"+hexString);
        System.out.println("转换为二进制：\n"+bytes2BinaryStr(str.getBytes()));

        byte [] bArray = hexStringToBinary(hexString);
        System.out.println("将str的十六进制文件转换为二进制再转为String：\n" + new String(bArray, StandardCharsets.UTF_8));
    }
    /**
     *
     * @param bArray
     * @return 转换为二进制字符串
     */
    public static String bytes2BinaryStr(byte[] bArray){

        String outStr = "";
        int pos = 0;
        for(byte b : bArray){
            //高四位
            pos = (b&0xF0)>>4;
            outStr += binaryArray[pos];
            //低四位
            pos = b&0x0F;
            outStr += binaryArray[pos];
        }
        return outStr;
    }
    /**
     *
     * @param bytes
     * @return 将二进制转换为十六进制字符输出
     */
    public static String binaryToHexString(byte[] bytes){
        String result = "";
        String hex = "";
        for(int i=0;i<bytes.length;i++){
            //字节高4位
            hex = String.valueOf(hexStr.charAt((bytes[i]&0xF0)>>4));
            //字节低4位
            hex += String.valueOf(hexStr.charAt(bytes[i]&0x0F));
            result += hex+"";
        }
        return result;
    }
    /**
     *
     * @param hexString
     * @return 将十六进制转换为字节数组
     */
    public static byte[] hexStringToBinary(String hexString){
        //hexString的长度对2取整，作为bytes的长度
        int len = hexString.length()/2;
        byte[] bytes = new byte[len];
        //字节高四位
        byte high = 0;
        //字节低四位
        byte low = 0;
        for(int i=0;i<len;i++){
            //右移四位得到高位
            high = (byte)((hexStr.indexOf(hexString.charAt(2*i)))<<4);
            low = (byte)hexStr.indexOf(hexString.charAt(2*i+1));
            //高地位做或运算
            bytes[i] = (byte) (high|low);
        }
        return bytes;
    }
}
